package com.crm.service.after_sale.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crm.dao.after_sale.ComplaintDao;
import com.crm.pojo.ClientComplaint;
import com.crm.service.after_sale.ComplaintService;
import com.crm.vo.Result;
@Service
@Transactional(rollbackFor = Exception.class)
public class ComplaintServiceImpl implements ComplaintService {

	@Autowired
	private ComplaintDao complaintDao;
	
	@Override
	public List<ClientComplaint> findComplaintBycomplaintId(Long complaintId, int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return complaintDao.findComplaintBycomplaintId(complaintId, page);
	}

	@Override
	public List<ClientComplaint> findComplaintByPage(int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return complaintDao.findComplaintByPage(page);
	}

	@Override
	public Long countComplaintByPage() {
		return complaintDao.countComplaintByPage();
	}

	@Override
	public Result addClientComplaint(ClientComplaint clientComplaint) {
		try {
			clientComplaint.setClientComplaintIsDel("否");
			complaintDao.save(clientComplaint);
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result("对不起，新增客户投诉失败！");
		}
	}

	@Override
	public Result delClientComplaint(Long complaintId) {
		try {
			ClientComplaint clientComplaint = complaintDao.findOne(complaintId);
			clientComplaint.setClientComplaintIsDel("是");
			complaintDao.save(clientComplaint);
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result("对不起，删除客户投诉失败！");
		}
	}

	@Override
	public Result editClientComplaint(ClientComplaint clientComplaint) {
		try {
			clientComplaint.setClientComplaintIsDel("否");
			complaintDao.save(clientComplaint);
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result("对不起，编辑客户投诉失败！");
		}
	}

	@Override
	public List<ClientComplaint> findByTheme(String theme, int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return complaintDao.findByTheme("%"+theme+"%", page);
	}

	@Override
	public Long countByTheme(String theme) {
		return complaintDao.countByTheme("%"+theme+"%");
	}

}
